<?php

class Score_CommentController extends Zend_Controller_Action
{
    private $_teacherSn;
    public function init()
    {
        // 教師代號
        $this->_teacherSn = Teacher_Model_Teacher::getSnById();
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }


    public function commentManagerAction()
    {
        $this->getHelper('layout')->disableLayout();
        // 評語類別
        $this->view->kindData = Doctrine_Query::create()
        ->from('Score_Model_CommentKind')
        ->where('teacher_sn=0 OR teacher_sn=?', $this->_teacherSn)
        ->orderBy('teacher_sn DESC')
        ->fetchArray();

        // 評語等級
        $this->view->levelData = Doctrine_Query::create()
        ->from('Score_Model_CommentLevel')
        ->where('teacher_sn=0 OR teacher_sn=?', $this->_teacherSn)
        ->orderBy('teacher_sn DESC')
        ->fetchArray();


    }

    /**
     * 列出自訂評語
     * @return unknown_type
     */
    public function commentListTableAction()
    {
        $this->getHelper('layout')->disableLayout();

        $kindSn = (int)$this->getRequest()->getParam('kindSn',0);
        $levelSn = (int)$this->getRequest()->getParam('levelSn',0);
        $this->view->kindSn = $kindSn;
        $this->view->levelSn = $levelSn;
        $query = Doctrine_Query::create()
        ->from('Score_Model_Comment')
        ->where('teacher_sn=?', $this->_teacherSn)
        ->andWhere('kind_sn=?', $kindSn)
        ->andWhere('level_sn=?', $levelSn)
        ->orderby('sn');


        $p = $this->getRequest()->getParam('p');
        $c = $this->getRequest()->getParam('c');
        // set page number from request
        $currentPage = isset($p) ? (int) htmlentities($p) : 1;

        // set number of items per page from request
        $itemsPerPage = isset($c) ? (int) htmlentities($c) : 10;

        // initialize pager
        $pager = new Doctrine_Pager($query, $currentPage, $itemsPerPage);

        // execute paged query
        $result = $pager->execute(array(), Doctrine::HYDRATE_ARRAY);

        // initialize pager layout
        $layout = new Doctrine_Pager_Layout(
        $pager,
        new Doctrine_Pager_Range_Sliding(array('chunk' => 5)),
  "?p={%page}&c=$itemsPerPage"
        );


        // set page link display template
        $layout->setTemplate('<a href="#" url="{%url}" page="{%page}">{%page}</a>');
        $layout->setSeparatorTemplate(' | ');

        $this->view->data = $result;
        $this->view->layout =$layout;
         $this->view->currentPage = $currentPage;
    }

    /**
     * 刪除評語
     * @return unknown_type
     */
    public function commentDeleteAction()
    {
        $sn = (int)$this->getRequest()->getParam('sn',0);
        $q = Doctrine_Query::create()
        ->delete('Score_Model_Comment')
        ->where('sn=?', $sn)
        ->andWhere('teacher_sn=?', $this->_teacherSn);
        $q->execute();
        $this->_helper->json(1);
    }

    /**
     * 儲存自訂評語
     * @return unknown_type
     */
    public function commentSaveAction()
    {
        $request = $this->getRequest();
        $kindSn = $request->getParam('kind_sn');
        $levelSn = $request->getParam('level_sn');
        $name = $request->getParam('name');

        $comment = new Score_Model_Comment();
        $form = new Score_Form_Comment();
        $data = array();
        $message = array();
        if ($name){
            foreach($name  as $sn=>$val) {
                unset($data);
                $data['kind_sn'] = $kindSn;
                $data['level_sn'] = $levelSn;
                $data['text'] = $val;
                $data['sn'] = $sn;
                $data['teacher_sn'] = $this->_teacherSn;
                if ($form->isValid($data)){
                    $comment->assignIdentifier($sn);
                    $comment->fromArray($data);
                    $comment->save();
                }
                else
                $message[] = $form->getMessages();
            }
        }
        // new data

        $name = $request->getParam('new_ManagerName');
        if ($name) {
            foreach($name  as $sn=>$val) {
                unset($data);
                $data['kind_sn'] = $kindSn;
                $data['level_sn'] = $levelSn;
                $data['text'] = $val;
                $data['teacher_sn'] = $this->_teacherSn;
                if ($form->isValid($data)){
                    $comment = new Score_Model_Comment();
                    $comment->fromArray($data);
                    $comment->save();
                }
                else
                $message[] = $form->getMessages();
            }
        }

        if ($message)
        $this->_helper->json(array('error'=>$message));
        else
        $this->_helper->json(array('success'=>1));
    }

    /**
     * 列出類別
     * @return unknown_type
     */
    public function commentKindListAction()
    {
        $this->getHelper('layout')->disableLayout();
        $this->view->data = Doctrine_Query::create()
        ->from('Score_Model_CommentKind')
        ->where('teacher_sn=0 OR teacher_sn=?', $this->_teacherSn)
        ->orderBy('sn')
        ->fetchArray();
    }

    /**
     * 儲存類別
     * @return unknown_type
     */
    public function commentKindSaveAction()
    {
        $request = $this->getRequest();
        $name = $request->getParam('name');

        $commentKind = new Score_Model_CommentKind();
        $form = new Score_Form_CommentKind();
        $data = array();
        $message = array();
        if ($name){
            foreach($name  as $sn=>$val) {
                unset($data);
                $data['name'] = $val;
                $data['sn'] = $sn;
                $data['teacher_sn'] = $this->_teacherSn;
                if ($form->isValid($data)){
                    $commentKind->assignIdentifier($sn);
                    $commentKind->fromArray($data);
                    $commentKind->save();
                }
                else
                $message[] = $form->getMessages();
            }
        }
        // new data

        $name = $request->getParam('new_KindName');
        if ($name) {
            foreach($name  as $sn=>$val) {
                unset($data);
                $data['name'] = $val;
                $data['teacher_sn'] = $this->_teacherSn;
                if ($form->isValid($data)){
                    $commentKind = new Score_Model_CommentKind();
                    $commentKind->fromArray($data);
                    $commentKind->save();
                }
                else
                $message[] = $form->getMessages();
            }
        }

        if ($message)
        $this->_helper->json(array('error'=>$message));
        else
        $this->_helper->json(array('success'=>1));
    }

    /**
     * 刪除類別
     * @return unknown_type
     */
    public function commentKindDeleteAction()
    {
        $sn = (int)$this->getRequest()->getParam('sn',0);
        $q = Doctrine_Query::create()
        ->delete('Score_Model_CommentKind')
        ->where('sn=?', $sn)
        ->andWhere('teacher_sn=?', $this->_teacherSn);
        $q->execute();
        $this->_helper->json(1);
    }

    /**
     * 列出等級
     * @return unknown_type
     */
    public function commentLevelListAction()
    {
        $this->getHelper('layout')->disableLayout();
        $this->view->data = Doctrine_Query::create()
        ->from('Score_Model_CommentLevel')
        ->where('teacher_sn=0 OR teacher_sn=?', $this->_teacherSn)
        ->orderBy('sn')
        ->fetchArray();
    }

    /**
     * 儲存類別
     * @return unknown_type
     */
    public function commentLevelSaveAction()
    {
        $request = $this->getRequest();
        $name = $request->getParam('name');

        $commentKind = new Score_Model_CommentLevel();
        $form = new Score_Form_CommentLevel();
        $data = array();
        $message = array();
        if ($name){
            foreach($name  as $sn=>$val) {
                unset($data);
                $data['name'] = $val;
                $data['sn'] = $sn;
                $data['teacher_sn'] = $this->_teacherSn;
                if ($form->isValid($data)){
                    $commentKind->assignIdentifier($sn);
                    $commentKind->fromArray($data);
                    $commentKind->save();
                }
                else
                $message[] = $form->getMessages();
            }
        }
        // new data

        $name = $request->getParam('new_LevelName');
        if ($name) {
            foreach($name  as $sn=>$val) {
                unset($data);
                $data['name'] = $val;
                $data['teacher_sn'] = $this->_teacherSn;
                if ($form->isValid($data)){
                    $commentKind = new Score_Model_CommentLevel();
                    $commentKind->fromArray($data);
                    $commentKind->save();
                }
                else
                $message[] = $form->getMessages();
            }
        }

        if ($message)
        $this->_helper->json(array('error'=>$message));
        else
        $this->_helper->json(array('success'=>1));
    }

    /**
     * 刪除類別
     * @return unknown_type
     */
    public function commentLevelDeleteAction()
    {
        $sn = (int)$this->getRequest()->getParam('sn',0);
        $q = Doctrine_Query::create()
        ->delete('Score_Model_CommentLevel')
        ->where('sn=?', $sn)
        ->andWhere('teacher_sn=?', $this->_teacherSn);
        $q->execute();
        $this->_helper->json(1);
    }
}

